programming4us
           
 
 
Programming

Software Testing with Visual Studio Team System 2008 : Data-driven unit testing

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/8/2011 6:10:45 PM
This type of testing is useful in carrying out the same test multiple times with different input data from a data source. The data source can have any number of records or data row for which we wanted the test to be carried out.

Instead of passing each data row values to the test application and executing entire test for each data row, we can link the test method to the data source. So when the test is run, the test method will take the data row one by one from the data source and will carry out the test for that many numbers of times with different input values.

This is similar to the web testing or load testing with data source attaching to the web method parameters. This could be used in case of testing number of user scenarios with different user logins to check the access permission or to see the validation based on the user roles, if anything is applicable to the application.

There are two different ways of configuring the data source or attaching the source to the test method. Let us consider one simple example of a method which takes two parameters as quantity and unit price. The result of the method would be returning the multiplied value of these two values and applying a percentage of tax on it.

public double CalculateTotalPrice(double uPrice, int Qty)
{
double totalPrice;
double tax = 0.125;
totalPrice = uPrice * Qty + (uPrice * tax * Qty); //
return totalPrice;
}

Create a unit test for the above example. The unit test code would contain the following code for the above method:

[TestMethod()]
public void CalculateTotalPriceTest()
{
Class1 target = new Class1();
double uPrice = 0F;
int Qty = 0;
double expected = 0F;
double actual;
actual = target.CalculateTotalPrice(uPrice, Qty);
Assert.AreEqual(expected, actual);
}

Before setting the properties, we have to create the data source. The data source can be of different format like CSV, XML, Microsoft Access, Microsoft SQL Server Database or Oracle Database, or any other database. For this example, we will consider a CSV file having five records with UnitPrice, Quantity, ExpectedTotalPrice. These are values required in the test method.

Now the new unit test would also be listed in the Test View and Test List Editor. Open the Test View or Test List Editor using the Test menu option from the IDE. Select the test method from the list and open the Properties window. From the list of properties listed for the unit test, select the connection property, and choose the option to open the Data Source wizard. From the wizard, select the data source type as CSV file from the options. Select the CSV file we created from the location. This will show the preview of the data too. Now the property of the test would be like the window shown below:

  • Data Provider Name: This property is disabled as we have selected the file directly and made the connection. Visual Studio automatically assigns the Data Provider Name as Microsoft.VisualStudio.TestTools.DataSource.CSV.

  • Data Table Name: After making the connection, we can see the tables listed from the database connected. The table that we select from the list will be the source of data for the testing.

  • Data Access Method: This can be Sequential or Dynamic. This is the method that will be used for retrieving the data from the data source for the test.

When we keep changing the properties of the test, we can see the properties added as attributes to the test method.

[DeploymentItem("TestProject\\Data.csv"), DataSource("Microsoft.VisualStudio.TestTools.DataSource. CSV", "|DataDirectory|\\Data.csv", 
"Data#csv", DataAccessMethod.Sequential), TestMethod()]
public void CalculateTotalPriceTest()
{
}



The data source which is a CSV file is added as the deployment item. The other attributes specify the method of data access and the namespace used. These are the method level attributes set for the test run.

To set the value of the data from the data source to the test method, modify the test method little bit as shown below. The testContextInstance.DataRow is used to fetch the value from the current row for the current instance of the test. For example, if we have five rows in the data source there would be five different instances of tests one for each row.

I have added a custom error message to the assert to get the actual and expected values in case if the test fails.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource. CSV", "|DataDirectory|\\Data.csv", "Data#csv", DataAccessMethod.Sequential), 
DeploymentItem("TestProject1\\Data.csv"), TestMethod()]
public void CalculateTotalPriceTest()
{
Class1 target = new Class1();
double uPrice = 0F;
int Qty = 0;
double expected = 0F;
double actual;
expected = Convert.ToDouble(testContextInstance. DataRow["ExpectedTotalPrice"]);
actual = target.CalculateTotalPrice(Convert.ToDouble (testContextInstance.DataRow["UnitPrice"]),
Convert.ToInt32(testContextInstance. DataRow["Quantity"]));
Assert.AreEqual(expected, actual, "The expected value is {0} but the actual value is {1}", expected, actual);
Trace.WriteLine("Expected:" + expected + "; Actual:"+ actual);
}



Now open the Test View window and select the test method listed in the window.

On running the test, we can see the test execution happening for each row in the data source. Once the test has been completed for all of the rows in the data source, we can see the test result based on the results of all individual tests. Even if one test fails, the end result of the test run will be a failure. To get the test run to pass, all of the individual tests within the selected test run should pass.

The output for the above test with the data source having five records in it, as shown in the previous screenshot, the test result would be:

We can see the total time taken for each test and the row picked for each test from the above result. All five tests fail and you can see the custom error message displayed with the expected and actual values applied to the formatters. All the tests fail because of the calculation mistake in the actual method. The data source contains the expected value based on the tax value as 0.12 but the actual method has the value as 0.125. If you change the value to 0.12 in the method CalculateTotalPriceTest and rerun the test, the test would pass.

The Test Run details window shows the status of each test run for each row in the data source.

Other -----------------
- Software Testing with Visual Studio Team System 2008 : Unit testing an ASP.NET application
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Replacing an Exception & Logging an Exception
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Diving in with a Simple Example
- iPhone Programming : Connecting to the Network - Embedding a Web Browser in Your App
- iPhone Programming : Connecting to the Network - Detecting Network Status
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Programming - Software Patterns
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Programming - Multicore Computing & Speedup
- Microsoft ASP.NET 3.5 : Web Services for ASP.NET AJAX Applications (part 2) - Consuming AJAX Web Services
- Microsoft ASP.NET 3.5 : Web Services for ASP.NET AJAX Applications (part 1) - Remote Calls via Web Services
- Microsoft ASP.NET 3.5 : AJAX-Enabled Web Services - Implementing the AJAX Paradigm
- The Art of SEO : Measuring Search Traffic (part 2)
- The Art of SEO : Measuring Search Traffic (part 1)
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Do Math
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Interact with Users
- Context and Interception : The .NET Context
- Context and Interception : .NET Component Services
- Optimizing for Vertical Search : Mobile, Video & Multimedia Search
- Programming WCF Services : Data Contracts - Collections (part 2) - The CollectionDataContract Attribute & Dictionaries
- Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections
- iPhone Programming : The Image Picker View Controller - Adding the Image Picker to the City Guide Application
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us